<?
function getmicrotime() { 
    list($usec, $sec) = explode(" ", microtime()); 
    return ((double)$usec + (double)$sec); 
}

function show_banner($row) {
	$banner_bo = new BannerBO();
	switch ($row[$banner_bo->fields['type']]) {
		case 0:
			?><img src="<?=BANNERS_URL.$row[$banner_bo->fields['image']]?>" alt="<?=$row[$banner_bo->fields['alt']]
			?>" <?if ($row[$banner_bo->fields['sizeX']]>0&&$row[$banner_bo->fields['sizeY']]>0) {
			?>width="<?=$row[$banner_bo->fields['sizeX']]
			?>" height="<?=$row[$banner_bo->fields['sizeY']]
			?>"<?
			}?> border="0" <? 
			if ($row[$banner_bo->fields['onClickType']]=="1"){
			?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row[$banner_bo->fields['id']]
			?>,<?=$row["bannerType"]?>,'<?=$row[$banner_bo->fields['onClickValue']]?>');"<?
			}?>><?
			break;
		case 1:
			?><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" <?
			if ($row["bannerSizeX"]>0&&$row["bannerSizeY"]>0) {
			?>WIDTH="<?=$row["bannerSizeX"]?>" HEIGHT="<?=$row["bannerSizeY"]?>" <?
			}?>id="b<?=$row["bannerID"]?>"<? 
			if ($row["bannerOnClickType"]=="1"){
			?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row["bannerID"]
			?>,<?=$row["bannerType"]?>,'<?=$row["bannerOnClickValue"]?>');"<?
			}?>>
<PARAM NAME=movie VALUE="<?=BANNERS_URL.$row[$banner_bo->fields['image']]?>">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>

<EMBED src="<?=BANNERS_URL.$row[$banner_bo->fields['image']]
?>" quality=high bgcolor=#FFFFFF <?
if ($row[$banner_bo->fields['sizeX']]>0&&$row[$banner_bo->fields['sizeY']]>0) {
?>WIDTH="<?=$row[$banner_bo->fields['sizeX']]?>" HEIGHT="<?=$row[$banner_bo->fields['sizeY']]
?>" <?
}?>
NAME="myMovieName" ALIGN="" TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT><?
			break;
		case 2:
			?><img src="<?=$row[$banner_bo->fields['URL']]
			?>" alt="<?=$row[$banner_bo->fields['alt']]?>" <?
			if ($row[$banner_bo->fields['sizeX']]>0&&$row[$banner_bo->fields['sizeY']]>0) {
			?>width="<?=$row[$banner_bo->fields['sizeX']]
			?>" height="<?=$row[$banner_bo->fields['sizeY']]?>" <?
			}?>border="0" <? 
			if ($banner_bo->fields['onClickType']=="1"){
			?> style="cursor:pointer;cursor:hand;" onclick="showA(<?=$row[$banner_bo->fields['id']]
			?>,<?=$row[$banner_bo->fields['type']]
			?>,'<?=$row[$banner_bo->fields['onClickValue']]?>');"<?
			}?>><?
			break;
		case 3:
			echo $row[$banner_bo->fields['script']];
			break;
	}
}

function show_banners($id) {
	global $db;
	$cond = "";
	$banner_bo = new BannerBO();
	$article_banner_bo = new ArticleBannerBO();
	$article_bo = new ArticleBO();
	
	$qq="SELECT {$banner_bo->fields['id']}, {$banner_bo->fields['URL']}, {$banner_bo->fields['image']}, ".
	"{$banner_bo->fields['alt']}, {$banner_bo->fields['sizeX']}, {$banner_bo->fields['sizeY']}, ".
	"{$banner_bo->fields['type']},  {$banner_bo->fields['imprLeft']}, {$banner_bo->fields['unlimImpr']}, ".
	"{$banner_bo->fields['onClickType']}, {$banner_bo->fields['onClickValue']}, {$article_banner_bo->fields['direction']}, ".
  "{$banner_bo->fields['script']} ".
	"FROM {$banner_bo->tableName} INNER JOIN {$article_banner_bo->tableName} ON ".
	"({$banner_bo->fields['id']}={$article_banner_bo->fields['banner']} ".
	"AND ({$banner_bo->fields['state']}=1 AND {$banner_bo->fields['finishTime']}>{$banner_bo->fields['access']} AND ".
	"(({$banner_bo->fields['unlimImpr']}=1 AND {$banner_bo->fields['imprLeft']}>0) OR {$banner_bo->fields['unlimImpr']}=0))) ".
	"WHERE {$article_banner_bo->fields['article']}=$id ".
	"ORDER BY {$article_banner_bo->fields['direction']}";
	
//	var_dump($qq);
//	echo "<br>";
	$result=$db->query($qq) or die ("Select failed ".__LINE__);
//	echo "<br>";
	$ids=array();

$count_res = mysql_num_rows($result);
if($count_res>0){?>
<!-- BANNERS-->
	<tr><td valign="top" align="center">
	  <table border="0" cellpadding="0" cellspacing="0">
<?
}
	while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) {
		if ($row[$article_banner_bo->fields['direction']]) {
?><tr><?}?><td align="center" valign="top"><?
		show_banner($row);
		array_push($ids,$row["bannerID"]);
?></td><?
		if ($row[$article_banner_bo->fields['direction']]) {?></tr>
<tr height=5><td></td></tr><?}?>
<?
	} // while
if($count_res>0){?>
	  </table>
	</td></tr>
<? }
	$tm=getmicrotime();
	if (count($ids)) {
  	$qq=sprintf("UPDATE {$banner_bo->tableName} SET {$banner_bo->fields['access']}='%01.4f', ".
  	"{$banner_bo->fields['imprLeft']}=IF ({$banner_bo->fields['unlimImpr']}=1,".
  	"{$banner_bo->fields['imprLeft']}-1,{$banner_bo->fields['imprLeft']}), ".
  	"{$banner_bo->fields['viewCount']}={$banner_bo->fields['viewCount']}+1 ".
  	"WHERE {$banner_bo->fields['id']} IN (".implode(',',$ids).")",$tm);
//		var_dump($qq);
	  $db->query($qq) or die ("Update failed ".__LINE__);
	}
}

function banner_frame() {
?><iframe name="hframe" id="hframe" width="0" height="0" style="display: none;"></iframe><?
}?>
